package com.zsy.controller;

import com.zsy.annoation.ReportLogAnno;
import com.zsy.pojo.Dept;
import com.zsy.pojo.Result;
import com.zsy.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-07-24 16:19
 **/
@Slf4j //日志 酸辣粉四份
@RequestMapping("/depts")
@RestController
public class DeptController {
//    private static final Logger log = org.slf4j.LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
    /**
     * 查询所有部门信息
     * @return
     */
    @GetMapping
    public Result getDept(){
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }
    /**
     * 根据id查询部门信息 通过请求url直接传递参数 使用{}来标识该路径的参数 @PathVariable
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable Integer id){
        log.info("根据id查询部门:{} ",id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }
    /**
     * 删除部门信息
     * @param
     * @return
     */
    @DeleteMapping
    @ReportLogAnno
    public Result deleteDeptById(@RequestParam("id") Integer deptId){ //如果请求参数名与形参变量名相同，直接定义方法形参即可接收。（省略@RequestParam）
//        System.out.println("根据ID删除部门: " + deptId);
        log.info("根据ID删除部门:{} ",deptId);
        Integer count = deptService.deleteDeptById(deptId);
        if (count==0){
            log.info("删除部门成功");
            return Result.success();
        }
        else
            return Result.error("对不起，当前部门下有员工，不能直接删除！");
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    @ReportLogAnno
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门:{} ",dept);
//        System.out.println("添加部门: " + dept);
        deptService.addDept(dept);
        return Result.success();
    }
    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PutMapping
    @ReportLogAnno
    public Result updateDept(@RequestBody Dept dept){
//        System.out.println("修改部门: " + dept);
        log.info("修改部门:{} ",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
